<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/7/27
 * Time: 13:34
 */

namespace Biz\model\activity;

use Biz\pockets\constInterface\ActivityInfo;
use Doraemon\pockets\datebase\BaseDbModel;

class AllActivityGoods extends BaseDbModel implements ActivityInfo
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'all_activity_goods';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    const STATUS_DESC = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_DELETE => '已删除'
    ];

    /**
     * 修改带有goods信息的方法 !!! 没有goods信息请请用下方修改活动属性方法 !!!
     * @param $activityId
     * @param $typeId
     * @param $goodsInfo
     * @return bool
     */
    public static function updateActivityGoods($activityId, $typeId, $goodsInfo)
    {
        $checkGoods = self::get('id', [
            'activity_id' => $activityId,
            'type' => $typeId,
            'status' => self::STATUS_NORMAL,
            'goods_id' => $goodsInfo['goods_id']
        ]);

        $goodsInfo['edittime'] = date('Y-m-d H:i:s');

        if($checkGoods) {
            $result = self::update($goodsInfo, [
                'AND' => [
                    'activity_id' => $activityId,
                    'type' => $typeId,
                    'goods_id' => $goodsInfo['goods_id'],
                ]
            ])->rowcount();
        } else {
            $goodsInfo['addtime'] = date('Y-m-d H:i:s');
            $result = self::insert($goodsInfo)->rowcount();
        }

        if($result) {
            return $goodsInfo['goods_id'];
        }
        return false;
    }

    /**
     * 修改活动属性方法
     * @param $activityId
     * @param $typeId
     * @param $activityAttr
     * @return bool
     */
    public static function updateActivity($activityId, $typeId, $activityAttr)
    {
        $check = self::get('id', [
            'activity_id' => $activityId,
            'type' => $typeId,
            'status' => self::STATUS_NORMAL,
        ]);

        $activityAttr['edittime'] = date('Y-m-d H:i:s');

        if($check) {
            $result = self::update($activityAttr, [
                'AND' => [
                    'activity_id' => $activityId,
                    'type' => $typeId,
                ]
            ])->rowcount();
        } else {
            $activityAttr['addtime'] = date('Y-m-d H:i:s');
            $result = self::insert($activityAttr)->rowcount();
        }

        if(!$result) {
            return false;
        }
        return true;
    }

    public static function delActivityGoods($activityId, $typeId)
    {
        $result = self::update(['status' => self::STATUS_DELETE, 'real_end_time' => date('Y-m-d H:i:s')], [
            'AND' => [
                'activity_id' => $activityId,
                'type' => $typeId
            ]
        ])->rowcount();

        if($result) {
            return true;
        }
        return false;
    }

    public static function delNotInGoodsIds($activityId, $typeId, array $goodsIds)
    {
        $result = self::update(['status' => self::STATUS_DELETE], [
            'AND' => [
                'activity_id' => $activityId,
                'type' => $typeId,
                'goods_id[!]' => $goodsIds
            ]
        ])->rowcount();

        if($result) {
            return true;
        }
        return false;
    }

    public static function getActivityInfoById($activityId, $typeId, $field = "*")
    {
        $time = date('Y-m-d H:i:s');
        return self::select($field, [
            'AND' => [
                'activity_id' => $activityId,
                'type_id' => $typeId,
                'start_time[<=]' => $time,
                'end_time[>=]' => $time,
                'status' => self::STATUS_NORMAL,
            ]
        ]);
    }

    public static function getActivityGoodsInfoByGoodsIds($goodsIds, $field = "*")
    {
        $time = date('Y-m-d H:i:s');

        return self::select($field, [
            'AND' => [
                'goods_id' => $goodsIds,
                'end_time[>=]' => $time,
                'status' => self::STATUS_NORMAL,
            ]
        ]);
    }

    public static function getActivityInfosByIds($activityIds, $typeId)
	{
		$infos = self::select('*', [
			'AND' => [
				'activity_id' => $activityIds,
				'type' => $typeId,
				'status' => self::STATUS_NORMAL,
			]
		]);

		return array_column($infos, null, 'activity_id');

	}

	public static function delActivityGoodsById($activityId, $typeId, $goodsId)
    {
        $isCanDel = self::get('id', [
            'activity_id' => $activityId,
            'type' => $typeId,
            'goods_id' => $goodsId,
            'status' => self::STATUS_NORMAL,
        ]);
        if(!$isCanDel) {
            return true;
        }

        $result = self::update(['status' => self::STATUS_DELETE, 'real_end_time' => date('Y-m-d H:i:s')], [
            'activity_id' => $activityId,
            'type' => $typeId,
            'goods_id' => $goodsId,
            'status' => self::STATUS_NORMAL
        ])->rowcount();

        if($result) {
            return true;
        }
        return false;
    }


//	/**
//	 * 获取未结束的变价活动信息 存入缓存
//	 * @param array $typeIds
//	 * @return mixed
//	 */
//    public static function getNotOverInfosByTypes($goodsId, $typeIds = [
//    	self::ACTIVITY_TYPE_LIMIT_TIME_ROB,
//		self::ACTIVITY_TYPE_WEEK_PURCHASE,
//		self::ACTIVITY_TYPE_NEWCOMER_EXCLUSIVE
//	])
//	{
//		$time = date('Y-m-d H:i:s');
//		return self::select('*', [
//			'goods_id' => $goodsId,
//			'type' => $typeIds,
//			'end_time[>]' => $time,
//			'status' => self::STATUS_NORMAL,
//		]);
//	}

    public static function getCheckActivityList($goodsId, $type, $activityId)
    {
        $where = [
            'goods_id' => $goodsId,
            'type' => $type,
            'status' => self::STATUS_NORMAL,
            'end_time[>=]' => date('Y-m-d H:i:s')
        ];
        if($activityId && $activityId > 0) {
            $where = array_merge($where, ['activity_id[!]' => $activityId]);
        }

        return self::select('*', [
            'AND' =>
                $where
        ]);
    }
}